#include"Include.h"

extern vu16 R_Iac_2048;
extern vu16 LimTargetPower;
extern vu8 buff,Check_Sum;
extern vu16 TargetTemp,PPGTA_buf;
extern vu8 PanDetPulseNum ;
extern vu8 OffCurrent_TH;
void main()
{
	GCC_DELAY(50000);
    ClearMemory();	
    System_Init();
    GCC_DELAY(1000);
    _emi = ENABLE;	


	while(1)
	{ 	
		
		GCC_CLRWDT();
//-----------------Multi-Tasks---------------------//		    
	    Surge_Protect();  //Inrush current protection	
		LowPow_PowerUpdata();				
		ADC_Task();
				
		if (bT1S_Event==1)
		{
		   bT1S_Event = 0;
		   
		   // DEBUG 
#ifdef UART_ASCII
		   	Debug_Code();
#endif		
		   OverTEMP_PowerDrop();   //IGBT Over temperature reduces power
		   NTC_Detect();           //NTC error detection
		   Protect_ErrRecovery();      //Error recovery  
		}
		//----- 100mS routine task ------
		if (bT100mS_Event==1)
		{
			bT100mS_Event=0;	
			PanMoveIn_Detect();    //Check pot
			PowerControl_Task(); 
			IIC_TimeOut();//I2C error			 
		}
		//----- 20mS routine task ------
		if (bT20mS_Event==1)
		{
			bT20mS_Event=0;	
			UI_Task(); //every 20mS	
			PanMoveOut_Detect(); //run every 20mS							
		}
		//----- 10mS routine task ------
		if(bT10mS_Event==1)
		{
			bT10mS_Event=0;			
		}		
		//----- 1mS routine task ------
	    if(1==bT1mS_Event)
		{
			bT1mS_Event=0;
			TimeBase() ;  //Subroutine execution time count		   
			ProtectWithAD_Task();//Some protection mechanism processing related to AD	    
			
			#if BUZZER_IO_TYPE == 0
				if(Beep_Cnt>0)
				{
					Beep_Cnt--;	
				}
				else
				{
					bBeep_ON = 0;	
				}
			#endif	
		}		
	}
}




void Debug_Code()
{
#ifdef UART_ASCII

//	uart_print_uint(PowerStep_buf);
//	uart_put_char(','); 
 	uart_print_uint(PowerStep);
  	uart_put_char(','); 
	uart_print_uint(LimTargetPower);
	uart_put_char(','); 
	uart_print_uint(TargetPower);
	uart_put_char(',');		
	uart_print_uint(AD_Vac);
	uart_put_char(',');
    uart_print_uint(AD_Iac);
    uart_put_char(',');
//	uart_print_uint(PPGTA_copy);
//	uart_put_char(',');
	uart_print_uint(R_PowerIV);
	uart_put_char(',');
	uart_print_uint(Temp_PAN);
	uart_put_char(',');
//	uart_print_uint(R_PanTemp);
//	uart_put_char(',');
//	uart_print_uint(TargetTemp);
//	uart_put_char(','); 
//	uart_print_uint(PanDetPulseNum);
//	uart_put_char(',');
	uart_print_uint(Temp_IGBT);
	uart_put_char(',');   
//	uart_print_uint(LimTargetPower);
//	uart_put_char(',');
//	uart_print_uint((R_Iac_2048/10));
//	uart_put_char(',');
	uart_print_uint(OffCurrent_TH);
	uart_put_char(',');
	uart_print_uint(u16ErrorBuf);

    uart_put_char(0x0d);
    uart_put_char(0x0A);
#endif
}


